package gugga.utils
{
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.Event;
	
	/**
	 * <code>DelayedCallTimeout</code> is an utility for executing methods after a specified 
	 * time interval in milliseconds.
	 */
	public class DelayedCallTimeout
	{
		private var mInterval :  Number;
		private var mArguments : Array;
		private var mObject : Object;
		private var mFunction : Function;
		private var mTimer : Timer
		
		public function DelayedCallTimeout(
			aTime : Number, 
			aObjectInstance : Object, 
			aFunction : Function) : void
		{	
			mFunction = aFunction;
			mArguments = arguments.splice(3);
			mObject = aObjectInstance;
			mTimer = new Timer(aTime);
			mTimer.addEventListener(TimerEvent.TIMER, solve);
			mTimer.start();
		}
		
		private function solve(aEvent : Event) : void 
		{
			clear();
			mFunction.apply(mObject, mArguments);
		}
		
		public function clear() : void 
		{
			mTimer.reset();
		}
		
		public static function create(
			aTime : Number, 
			aObjectInstance : Object, 
			aFunction : Function) : DelayedCallTimeout
		{
			return new DelayedCallTimeout(aTime, aObjectInstance, aFunction);
		}
	}
}